package com.inei.evaluaciondelaencuesta.view;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.inei.evaluaciondelaencuesta.R;
import com.inei.evaluaciondelaencuesta.controller.RespuestaEncuestaController;
import com.inei.evaluaciondelaencuesta.model.entity.RespuestaEncuesta;
import com.inei.evaluaciondelaencuesta.tools.Manejo;
import com.inei.evaluaciondelaencuesta.tools.SpinnerUtil;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class Encuesta_X_Activity extends Activity {

	// variables componentes
	private RadioButton rbDirecta, rbIndirecta;
	private Spinner spErrorC10, spError142, spError143, spError144, spError145,
			spError146, spError147, spError148, spError149, spError150,
			spError151, spError152, spError153, spError154, spError155,
			spError156, spError157, spLectura142, spLectura143, spLectura144,
			spLectura145, spLectura146, spLectura147, spLectura148,
			spLectura149, spLectura150, spLectura151, spLectura152,
			spLectura153, spLectura154, spLectura155, spLectura156,
			spLectura157, spComprension142, spComprension143, spComprension144,
			spComprension145, spComprension146, spComprension147,
			spComprension148, spComprension149, spComprension150,
			spComprension151, spComprension152, spComprension153,
			spComprension154, spComprension155, spComprension156,
			spComprension157, spOtro142, spOtro143, spOtro144, spOtro145,
			spOtro146, spOtro147, spOtro148, spOtro149, spOtro150, spOtro151,
			spOtro152, spOtro153, spOtro154, spOtro155, spOtro156, spOtro157;
	private EditText etOBSC10, etHIC10, etHTC10, etTC10, etOBSCreditos,
			etHIC10_1, etHTC10_1, etTC10_1, etOtro142, etOtro143, etOtro144,
			etOtro145, etOtro146, etOtro147, etOtro148, etOtro149, etOtro150,
			etOtro151, etOtro152, etOtro153, etOtro154, etOtro155, etOtro156,
			etOtro157, etPregunta142, etPregunta143, etPregunta144,
			etPregunta145, etPregunta146, etPregunta147, etPregunta148,
			etPregunta149, etPregunta150, etPregunta151, etPregunta152,
			etPregunta153, etPregunta154, etPregunta155, etPregunta156,
			etPregunta157, etOBS142, etOBS143, etOBS144, etOBS145, etOBS146,
			etOBS147, etOBS148, etOBS149, etOBS150, etOBS151, etOBS152,
			etOBS153, etOBS154, etOBS155, etOBS156, etOBS157;
	private SpinnerUtil util;

	// variables
	private RespuestaEncuestaController con;
	private List<RespuestaEncuesta> lista;
	private int marco_id, formato_id;
	private String conglomerado, grupo, vivienda, hogar_id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_encuesta_x);

		con = new RespuestaEncuestaController(Encuesta_X_Activity.this);
		util = new SpinnerUtil(this);

		IniciarComponentes();

		AgregarEventos();

		CargarDatos();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.encuesta_i, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mnGuardar:
			GuardarDatos();
			if (lista.get(0).getRespuesta_id() > 0) {
				if (con.RespuestaEncuestaModificar(lista) > 0) {
					Intent ir_marco = new Intent(this,
							Encuesta_Persona_Activity.class);
					Bundle datos = getIntent().getExtras();
					datos.putInt("viene", 11);
					ir_marco.putExtras(datos);
					startActivity(ir_marco);

					Toast.makeText(this, "Se actualizo con exito",
							Toast.LENGTH_SHORT).show();
					finish();
				} else {
					Toast.makeText(this, "Error al actualizar",
							Toast.LENGTH_SHORT).show();
				}
			} else {
				if (con.RespuestaEncuestaAdicionar(lista) > 0) {
					Intent ir_marco = new Intent(this,
							Encuesta_Persona_Activity.class);
					Bundle datos = getIntent().getExtras();
					datos.putInt("viene", 11);
					ir_marco.putExtras(datos);
					startActivity(ir_marco);

					Toast.makeText(this, "Se guardo con exito",
							Toast.LENGTH_SHORT).show();
					finish();
				} else {
					Toast.makeText(this, "Error al guardar", Toast.LENGTH_SHORT)
							.show();
				}
			}
			break;
		case R.id.mnSalir:
			finish();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		Intent ir_encuesta = new Intent(this, Encuesta_IX_3_Activity.class);
		Bundle datos = getIntent().getExtras();
		ir_encuesta.putExtras(datos);
		startActivity(ir_encuesta);
		finish();

		super.onBackPressed();
	}

	private void IniciarComponentes() {
		rbDirecta = (RadioButton) findViewById(R.id.rbDirecta);
		rbIndirecta = (RadioButton) findViewById(R.id.rbIndirecta);

		spErrorC10 = (Spinner) findViewById(R.id.spErrorC10);
		spError142 = (Spinner) findViewById(R.id.spError142);
		spError143 = (Spinner) findViewById(R.id.spError143);
		spError144 = (Spinner) findViewById(R.id.spError144);
		spError145 = (Spinner) findViewById(R.id.spError145);
		spError146 = (Spinner) findViewById(R.id.spError146);
		spError147 = (Spinner) findViewById(R.id.spError147);
		spError148 = (Spinner) findViewById(R.id.spError148);
		spError149 = (Spinner) findViewById(R.id.spError149);
		spError150 = (Spinner) findViewById(R.id.spError150);
		spError151 = (Spinner) findViewById(R.id.spError151);
		spError152 = (Spinner) findViewById(R.id.spError152);
		spError153 = (Spinner) findViewById(R.id.spError153);
		spError154 = (Spinner) findViewById(R.id.spError154);
		spError155 = (Spinner) findViewById(R.id.spError155);
		spError156 = (Spinner) findViewById(R.id.spError156);
		spError157 = (Spinner) findViewById(R.id.spError157);
		spLectura142 = (Spinner) findViewById(R.id.spLectura142);
		spLectura143 = (Spinner) findViewById(R.id.spLectura143);
		spLectura144 = (Spinner) findViewById(R.id.spLectura144);
		spLectura145 = (Spinner) findViewById(R.id.spLectura145);
		spLectura146 = (Spinner) findViewById(R.id.spLectura146);
		spLectura147 = (Spinner) findViewById(R.id.spLectura147);
		spLectura148 = (Spinner) findViewById(R.id.spLectura148);
		spLectura149 = (Spinner) findViewById(R.id.spLectura149);
		spLectura150 = (Spinner) findViewById(R.id.spLectura150);
		spLectura151 = (Spinner) findViewById(R.id.spLectura151);
		spLectura152 = (Spinner) findViewById(R.id.spLectura152);
		spLectura153 = (Spinner) findViewById(R.id.spLectura153);
		spLectura154 = (Spinner) findViewById(R.id.spLectura154);
		spLectura155 = (Spinner) findViewById(R.id.spLectura155);
		spLectura156 = (Spinner) findViewById(R.id.spLectura156);
		spLectura157 = (Spinner) findViewById(R.id.spLectura157);
		spComprension142 = (Spinner) findViewById(R.id.spComprension142);
		spComprension143 = (Spinner) findViewById(R.id.spComprension143);
		spComprension144 = (Spinner) findViewById(R.id.spComprension144);
		spComprension145 = (Spinner) findViewById(R.id.spComprension145);
		spComprension146 = (Spinner) findViewById(R.id.spComprension146);
		spComprension147 = (Spinner) findViewById(R.id.spComprension147);
		spComprension148 = (Spinner) findViewById(R.id.spComprension148);
		spComprension149 = (Spinner) findViewById(R.id.spComprension149);
		spComprension150 = (Spinner) findViewById(R.id.spComprension150);
		spComprension151 = (Spinner) findViewById(R.id.spComprension151);
		spComprension152 = (Spinner) findViewById(R.id.spComprension152);
		spComprension153 = (Spinner) findViewById(R.id.spComprension153);
		spComprension154 = (Spinner) findViewById(R.id.spComprension154);
		spComprension155 = (Spinner) findViewById(R.id.spComprension155);
		spComprension156 = (Spinner) findViewById(R.id.spComprension156);
		spComprension157 = (Spinner) findViewById(R.id.spComprension157);
		spOtro142 = (Spinner) findViewById(R.id.spOtro142);
		spOtro143 = (Spinner) findViewById(R.id.spOtro143);
		spOtro144 = (Spinner) findViewById(R.id.spOtro144);
		spOtro145 = (Spinner) findViewById(R.id.spOtro145);
		spOtro146 = (Spinner) findViewById(R.id.spOtro146);
		spOtro147 = (Spinner) findViewById(R.id.spOtro147);
		spOtro148 = (Spinner) findViewById(R.id.spOtro148);
		spOtro149 = (Spinner) findViewById(R.id.spOtro149);
		spOtro150 = (Spinner) findViewById(R.id.spOtro150);
		spOtro151 = (Spinner) findViewById(R.id.spOtro151);
		spOtro152 = (Spinner) findViewById(R.id.spOtro152);
		spOtro153 = (Spinner) findViewById(R.id.spOtro153);
		spOtro154 = (Spinner) findViewById(R.id.spOtro154);
		spOtro155 = (Spinner) findViewById(R.id.spOtro155);
		spOtro156 = (Spinner) findViewById(R.id.spOtro156);
		spOtro157 = (Spinner) findViewById(R.id.spOtro157);

		etOBSC10 = (EditText) findViewById(R.id.etOBSC10);
		etHIC10 = (EditText) findViewById(R.id.etHIC10);
		etHTC10 = (EditText) findViewById(R.id.etHTC10);
		etTC10 = (EditText) findViewById(R.id.etTC10);
		etOBSCreditos = (EditText) findViewById(R.id.etOBSCreditos);
		etHIC10_1 = (EditText) findViewById(R.id.etHIC10_1);
		etHTC10_1 = (EditText) findViewById(R.id.etHTC10_1);
		etTC10_1 = (EditText) findViewById(R.id.etTC10_1);
		etOtro142 = (EditText) findViewById(R.id.etOtro142);
		etOtro143 = (EditText) findViewById(R.id.etOtro143);
		etOtro144 = (EditText) findViewById(R.id.etOtro144);
		etOtro145 = (EditText) findViewById(R.id.etOtro145);
		etOtro146 = (EditText) findViewById(R.id.etOtro146);
		etOtro147 = (EditText) findViewById(R.id.etOtro147);
		etOtro148 = (EditText) findViewById(R.id.etOtro148);
		etOtro149 = (EditText) findViewById(R.id.etOtro149);
		etOtro150 = (EditText) findViewById(R.id.etOtro150);
		etOtro151 = (EditText) findViewById(R.id.etOtro151);
		etOtro152 = (EditText) findViewById(R.id.etOtro152);
		etOtro153 = (EditText) findViewById(R.id.etOtro153);
		etOtro154 = (EditText) findViewById(R.id.etOtro154);
		etOtro155 = (EditText) findViewById(R.id.etOtro155);
		etOtro156 = (EditText) findViewById(R.id.etOtro156);
		etOtro157 = (EditText) findViewById(R.id.etOtro157);
		etPregunta142 = (EditText) findViewById(R.id.etPregunta142);
		etPregunta143 = (EditText) findViewById(R.id.etPregunta143);
		etPregunta144 = (EditText) findViewById(R.id.etPregunta144);
		etPregunta145 = (EditText) findViewById(R.id.etPregunta145);
		etPregunta146 = (EditText) findViewById(R.id.etPregunta146);
		etPregunta147 = (EditText) findViewById(R.id.etPregunta147);
		etPregunta148 = (EditText) findViewById(R.id.etPregunta148);
		etPregunta149 = (EditText) findViewById(R.id.etPregunta149);
		etPregunta150 = (EditText) findViewById(R.id.etPregunta150);
		etPregunta151 = (EditText) findViewById(R.id.etPregunta151);
		etPregunta152 = (EditText) findViewById(R.id.etPregunta152);
		etPregunta153 = (EditText) findViewById(R.id.etPregunta153);
		etPregunta154 = (EditText) findViewById(R.id.etPregunta154);
		etPregunta155 = (EditText) findViewById(R.id.etPregunta155);
		etPregunta156 = (EditText) findViewById(R.id.etPregunta156);
		etPregunta157 = (EditText) findViewById(R.id.etPregunta157);
		etOBS142 = (EditText) findViewById(R.id.etOBS142);
		etOBS143 = (EditText) findViewById(R.id.etOBS143);
		etOBS144 = (EditText) findViewById(R.id.etOBS144);
		etOBS145 = (EditText) findViewById(R.id.etOBS145);
		etOBS146 = (EditText) findViewById(R.id.etOBS146);
		etOBS147 = (EditText) findViewById(R.id.etOBS147);
		etOBS148 = (EditText) findViewById(R.id.etOBS148);
		etOBS149 = (EditText) findViewById(R.id.etOBS149);
		etOBS150 = (EditText) findViewById(R.id.etOBS150);
		etOBS151 = (EditText) findViewById(R.id.etOBS151);
		etOBS152 = (EditText) findViewById(R.id.etOBS152);
		etOBS153 = (EditText) findViewById(R.id.etOBS153);
		etOBS154 = (EditText) findViewById(R.id.etOBS154);
		etOBS155 = (EditText) findViewById(R.id.etOBS155);
		etOBS156 = (EditText) findViewById(R.id.etOBS156);
		etOBS157 = (EditText) findViewById(R.id.etOBS157);

		util.spinnerLECTURA(spLectura142, spLectura143, spLectura144,
				spLectura145, spLectura146, spLectura147, spLectura148,
				spLectura149, spLectura150, spLectura151, spLectura152,
				spLectura153, spLectura154, spLectura155, spLectura156,
				spLectura157);
		util.spinnerCOMPRENSION(spComprension142, spComprension143,
				spComprension144, spComprension145, spComprension146,
				spComprension147, spComprension148, spComprension149,
				spComprension150, spComprension151, spComprension152,
				spComprension153, spComprension154, spComprension155,
				spComprension156, spComprension157);
		util.spinnerOTROS(spOtro142, spOtro143, spOtro144, spOtro145,
				spOtro146, spOtro147, spOtro148, spOtro149, spOtro150,
				spOtro151, spOtro152, spOtro153, spOtro154, spOtro155,
				spOtro156, spOtro157);
		util.spinnerERRORES(spErrorC10, spError142, spError143, spError144,
				spError145, spError146, spError147, spError148, spError149,
				spError150, spError151, spError152, spError153, spError154,
				spError155, spError156, spError157);
	}

	private void CargarDatos() {
		marco_id = getIntent().getExtras().getInt("marco_id");
		formato_id = getIntent().getExtras().getInt("formato_id");
		conglomerado = getIntent().getExtras().getString("conglomerado");
		grupo = getIntent().getExtras().getString("grupo");
		vivienda = getIntent().getExtras().getString("vivienda");
		hogar_id = getIntent().getExtras().getString("hogar_id");

		lista = con.RespuestaEncuestaBuscarCap(marco_id, formato_id, 10,
				hogar_id);

		if (lista != null) {
			// Capitulo X
			spErrorC10
					.setSelection(Integer.parseInt(lista.get(0).getP_ERROR()));
			etOBSC10.setText(lista.get(0).getP_OBS());
			if (lista.get(0).getP_IH() != null
					&& lista.get(0).getP_IM() != null) {
				etHIC10.setText(lista.get(0).getP_IH() + ":"
						+ lista.get(0).getP_IM());
			}

			if (lista.get(0).getP_FH() != null
					&& lista.get(0).getP_FM() != null) {
				etHTC10.setText(lista.get(0).getP_FH() + ":"
						+ lista.get(0).getP_FM());
			}
			etTC10.setText(lista.get(0).getP_TM() + "");

			if (lista.get(0).getP_DIREC() != null) {
				if (!lista.get(0).getP_DIREC().equals("0")) {
					rbDirecta.setChecked(isChecked(lista.get(0).getP_DIREC(),
							"1"));
					rbIndirecta.setChecked(isChecked(lista.get(0).getP_DIREC(),
							"2"));
				}
			}

			// Capitulo X Pregunta 142
			CargarPregunta(1, spLectura142, spComprension142, spOtro142,
					etOtro142, etPregunta142, spError142, etOBS142);

			// Capitulo X Pregunta 143
			CargarPregunta(2, spLectura143, spComprension143, spOtro143,
					etOtro143, etPregunta143, spError143, etOBS143);

			// Capitulo X Pregunta 144
			CargarPregunta(3, spLectura144, spComprension144, spOtro144,
					etOtro144, etPregunta144, spError144, etOBS144);

			// Capitulo X Pregunta 145
			CargarPregunta(4, spLectura145, spComprension145, spOtro145,
					etOtro145, etPregunta145, spError145, etOBS145);

			// Capitulo X Pregunta 146
			CargarPregunta(5, spLectura146, spComprension146, spOtro146,
					etOtro146, etPregunta146, spError146, etOBS146);

			// Capitulo X Pregunta 147
			CargarPregunta(6, spLectura147, spComprension147, spOtro147,
					etOtro147, etPregunta147, spError147, etOBS147);

			// Capitulo X Seccion 1
			etOBSCreditos.setText(lista.get(7).getP_OBS());
			if (lista.get(7).getP_IH() != null
					&& lista.get(7).getP_IM() != null) {
				etHIC10_1.setText(lista.get(7).getP_IH() + ":"
						+ lista.get(7).getP_IM());
			}

			if (lista.get(7).getP_FH() != null
					&& lista.get(7).getP_FM() != null) {
				etHTC10_1.setText(lista.get(7).getP_FH() + ":"
						+ lista.get(7).getP_FM());
			}
			etTC10_1.setText(lista.get(7).getP_TM() + "");

			// Capitulo X Pregunta 148
			CargarPregunta(8, spLectura148, spComprension148, spOtro148,
					etOtro148, etPregunta148, spError148, etOBS148);

			// Capitulo X Pregunta 149
			CargarPregunta(9, spLectura149, spComprension149, spOtro149,
					etOtro149, etPregunta149, spError149, etOBS149);

			// Capitulo X Pregunta 150
			CargarPregunta(10, spLectura150, spComprension150, spOtro150,
					etOtro150, etPregunta150, spError150, etOBS150);

			// Capitulo X Pregunta 151
			CargarPregunta(11, spLectura151, spComprension151, spOtro151,
					etOtro151, etPregunta151, spError151, etOBS151);

			// Capitulo X Pregunta 152
			CargarPregunta(12, spLectura152, spComprension152, spOtro152,
					etOtro152, etPregunta152, spError152, etOBS152);

			// Capitulo X Pregunta 153
			CargarPregunta(13, spLectura153, spComprension153, spOtro153,
					etOtro153, etPregunta153, spError153, etOBS153);

			// Capitulo X Pregunta 154
			CargarPregunta(14, spLectura154, spComprension154, spOtro154,
					etOtro154, etPregunta154, spError154, etOBS154);

			// Capitulo X Pregunta 155
			CargarPregunta(15, spLectura155, spComprension155, spOtro155,
					etOtro155, etPregunta155, spError155, etOBS155);

			// Capitulo X Pregunta 156
			CargarPregunta(16, spLectura156, spComprension156, spOtro156,
					etOtro156, etPregunta156, spError156, etOBS156);

			// Capitulo X Pregunta 157
			CargarPregunta(17, spLectura157, spComprension157, spOtro157,
					etOtro157, etPregunta157, spError157, etOBS157);
		}
	}

	private void AgregarEventos() {
		spOtro142.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				if (pos == 3) {
					mostrar(etOtro142);
				} else {
					ocultar(etOtro142);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> adapter) {

			}
		});

		spOtro143.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				if (pos == 3) {
					mostrar(etOtro143);
				} else {
					ocultar(etOtro143);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> adapter) {

			}
		});

		spOtro144.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				if (pos == 3) {
					mostrar(etOtro144);
				} else {
					ocultar(etOtro144);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> adapter) {

			}
		});

		spOtro145.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				if (pos == 3) {
					mostrar(etOtro145);
				} else {
					ocultar(etOtro145);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> adapter) {

			}
		});

		spOtro146.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				if (pos == 3) {
					mostrar(etOtro146);
				} else {
					ocultar(etOtro146);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> adapter) {

			}
		});

		spOtro147.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				if (pos == 3) {
					mostrar(etOtro147);
				} else {
					ocultar(etOtro147);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> adapter) {

			}
		});

		spOtro148.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				if (pos == 3) {
					mostrar(etOtro148);
				} else {
					ocultar(etOtro148);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> adapter) {

			}
		});

		spOtro149.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				if (pos == 3) {
					mostrar(etOtro149);
				} else {
					ocultar(etOtro149);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> adapter) {

			}
		});

		spOtro150.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				if (pos == 3) {
					mostrar(etOtro150);
				} else {
					ocultar(etOtro150);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> adapter) {

			}
		});

		spOtro151.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				if (pos == 3) {
					mostrar(etOtro151);
				} else {
					ocultar(etOtro151);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> adapter) {

			}
		});

		spOtro152.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				if (pos == 3) {
					mostrar(etOtro152);
				} else {
					ocultar(etOtro152);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> adapter) {

			}
		});

		spOtro153.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				if (pos == 3) {
					mostrar(etOtro153);
				} else {
					ocultar(etOtro153);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> adapter) {

			}
		});

		spOtro154.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				if (pos == 3) {
					mostrar(etOtro154);
				} else {
					ocultar(etOtro154);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> adapter) {

			}
		});

		spOtro155.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				if (pos == 3) {
					mostrar(etOtro155);
				} else {
					ocultar(etOtro155);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> adapter) {

			}
		});

		spOtro156.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				if (pos == 3) {
					mostrar(etOtro156);
				} else {
					ocultar(etOtro156);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> adapter) {

			}
		});

		spOtro157.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				if (pos == 3) {
					mostrar(etOtro157);
				} else {
					ocultar(etOtro157);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> adapter) {

			}
		});

		etHIC10.setFocusable(false);
		etHIC10.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EditText et = (EditText) v;
				AbrirTime(et.getText().toString(), et, 0, "");
			}
		});

		etHTC10.setFocusable(false);
		etHTC10.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EditText et = (EditText) v;
				AbrirTime(et.getText().toString(), et, 0, "");
			}
		});

		etHIC10_1.setFocusable(false);
		etHIC10_1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EditText et = (EditText) v;
				AbrirTime(et.getText().toString(), et, 0, "");
			}
		});

		etHTC10_1.setFocusable(false);
		etHTC10_1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EditText et = (EditText) v;
				AbrirTime(et.getText().toString(), et, 0, "");
			}
		});
	}

	private void GuardarDatos() {
		if (lista == null) {
			lista = new ArrayList<RespuestaEncuesta>();
			for (int i = 0; i < 18; i++) {
				RespuestaEncuesta x = new RespuestaEncuesta();
				x.setFormato_id(formato_id);
				x.setMarco_id(marco_id);
				x.setConglomerado(conglomerado);
				x.setGrupo(grupo);
				x.setVivienda(vivienda);
				x.setHogar(hogar_id);
				x.setTipo_reg("");
				x.setOrden("");
				x.setCap("10");
				lista.add(x);
			}

			// Capitulo X
			lista.get(0).setTipo("1");
			lista.get(0).setCodigo("");
			lista.get(0).setP_ERROR(spErrorC10.getSelectedItemPosition() + "");
			lista.get(0).setP_OBS(etOBSC10.getText().toString());
			if (etHIC10.getText().toString().length() != 0) {
				String[] iC10 = etHIC10.getText().toString().split(":");
				lista.get(0).setP_IH(iC10[0]);
				lista.get(0).setP_IM(iC10[1]);
			} else {
				lista.get(0).setP_IH(null);
				lista.get(0).setP_IM(null);
			}
			if (etHTC10.getText().toString().length() != 0) {
				String[] iC10 = etHTC10.getText().toString().split(":");
				lista.get(0).setP_FH(iC10[0]);
				lista.get(0).setP_FM(iC10[1]);
			} else {
				lista.get(0).setP_FH(null);
				lista.get(0).setP_FM(null);
			}
			lista.get(0).setP_TM(
					etTC10.getText().toString().length() == 0 ? 0 : Integer
							.parseInt(etTC10.getText().toString()));
			lista.get(0).setP_DIREC(
					rbDirecta.isChecked() ? "1"
							: (rbIndirecta.isChecked() ? "2" : "0"));

			// Capitulo X Pregunta 142
			GuardarPregunta(1, "3", "142", spLectura142, spComprension142,
					spOtro142, etOtro142, etPregunta142, spError142, etOBS142);

			// Capitulo X Pregunta 143
			GuardarPregunta(2, "3", "143", spLectura143, spComprension143,
					spOtro143, etOtro143, etPregunta143, spError143, etOBS143);

			// Capitulo X Pregunta 144
			GuardarPregunta(3, "3", "144", spLectura144, spComprension144,
					spOtro144, etOtro144, etPregunta144, spError144, etOBS144);

			// Capitulo X Pregunta 145
			GuardarPregunta(4, "3", "145", spLectura145, spComprension145,
					spOtro145, etOtro145, etPregunta145, spError145, etOBS145);

			// Capitulo X Pregunta 146
			GuardarPregunta(5, "3", "146", spLectura146, spComprension146,
					spOtro146, etOtro146, etPregunta146, spError146, etOBS146);

			// Capitulo X Pregunta 147
			GuardarPregunta(6, "3", "147", spLectura147, spComprension147,
					spOtro147, etOtro147, etPregunta147, spError147, etOBS147);

			// Capitulo X Seccion 1
			lista.get(7).setTipo("2");
			lista.get(7).setCodigo("1");
			lista.get(7).setP_OBS(etOBSCreditos.getText().toString());
			if (etHIC10_1.getText().toString().length() != 0) {
				String[] iC10_1 = etHIC10_1.getText().toString().split(":");
				lista.get(7).setP_IH(iC10_1[0]);
				lista.get(7).setP_IM(iC10_1[1]);
			} else {
				lista.get(7).setP_IH(null);
				lista.get(7).setP_IM(null);
			}
			if (etHTC10_1.getText().toString().length() != 0) {
				String[] iC10_1 = etHTC10_1.getText().toString().split(":");
				lista.get(7).setP_FH(iC10_1[0]);
				lista.get(7).setP_FM(iC10_1[1]);
			} else {
				lista.get(7).setP_FH(null);
				lista.get(7).setP_FM(null);
			}
			lista.get(7).setP_TM(
					etTC10_1.getText().toString().length() == 0 ? 0 : Integer
							.parseInt(etTC10_1.getText().toString()));

			// Capitulo X Pregunta 148
			GuardarPregunta(8, "3", "148", spLectura148, spComprension148,
					spOtro148, etOtro148, etPregunta148, spError148, etOBS148);

			// Capitulo X Pregunta 149
			GuardarPregunta(9, "3", "149", spLectura149, spComprension149,
					spOtro149, etOtro149, etPregunta149, spError149, etOBS149);

			// Capitulo X Pregunta 150
			GuardarPregunta(10, "3", "150", spLectura150, spComprension150,
					spOtro150, etOtro150, etPregunta150, spError150, etOBS150);

			// Capitulo X Pregunta 151
			GuardarPregunta(11, "3", "151", spLectura151, spComprension151,
					spOtro151, etOtro151, etPregunta151, spError151, etOBS151);

			// Capitulo X Pregunta 152
			GuardarPregunta(12, "3", "152", spLectura152, spComprension152,
					spOtro152, etOtro152, etPregunta152, spError152, etOBS152);

			// Capitulo X Pregunta 153
			GuardarPregunta(13, "3", "153", spLectura153, spComprension153,
					spOtro153, etOtro153, etPregunta153, spError153, etOBS153);

			// Capitulo X Pregunta 154
			GuardarPregunta(14, "3", "154", spLectura154, spComprension154,
					spOtro154, etOtro154, etPregunta154, spError154, etOBS154);

			// Capitulo X Pregunta 155
			GuardarPregunta(15, "3", "155", spLectura155, spComprension155,
					spOtro155, etOtro155, etPregunta155, spError155, etOBS155);

			// Capitulo X Pregunta 156
			GuardarPregunta(16, "3", "156", spLectura156, spComprension156,
					spOtro156, etOtro156, etPregunta156, spError156, etOBS156);

			// Capitulo X Pregunta 157
			GuardarPregunta(17, "3", "157", spLectura157, spComprension157,
					spOtro157, etOtro157, etPregunta157, spError157, etOBS157);
		}
	}

	private void CargarPregunta(int pos, Spinner spL, Spinner spC, Spinner spO,
			EditText etOT, EditText etP, Spinner spE, EditText etOB) {
		spL.setSelection(Integer.parseInt(lista.get(pos).getP_LECTU()));
		spC.setSelection(Integer.parseInt(lista.get(pos).getP_COMPR()));
		spO.setSelection(Integer.parseInt(lista.get(pos).getP_OTRO()));
		etOT.setText(lista.get(pos).getP_OTRO_ESP());
		etP.setText(lista.get(pos).getP_REFOR());
		spE.setSelection(Integer.parseInt(lista.get(pos).getP_ERROR()));
		etOB.setText(lista.get(pos).getP_OBS());
	}

	private void GuardarPregunta(int pos, String t, String c, Spinner spL,
			Spinner spC, Spinner spOT, EditText etOT, EditText etP,
			Spinner spE, EditText etOB) {
		lista.get(pos).setTipo(t);
		lista.get(pos).setCodigo(c);
		lista.get(pos).setP_LECTU(spL.getSelectedItemPosition() + "");
		lista.get(pos).setP_COMPR(spC.getSelectedItemPosition() + "");
		lista.get(pos).setP_OTRO(spOT.getSelectedItemPosition() + "");
		lista.get(pos).setP_OTRO_ESP(etOT.getText().toString());
		lista.get(pos).setP_REFOR(etP.getText().toString());
		lista.get(pos).setP_ERROR(spE.getSelectedItemPosition() + "");
		lista.get(pos).setP_OBS(etOB.getText().toString());
	}

	private boolean isChecked(String ind, String i) {
		return ind.equals(i) ? true : false;
	}

	private void mostrar(EditText et) {
		et.setVisibility(View.VISIBLE);

	}

	private void ocultar(EditText et) {
		et.setVisibility(View.GONE);
		et.setText("");
	}

	private void AbrirTime(String h, final EditText et, final int sw,
			final String h1) {

		final Calendar c = Calendar.getInstance();
		int horas = c.get(Calendar.HOUR_OF_DAY);
		int minutos = c.get(Calendar.MINUTE);
		if (h.length() != 0) {
			String valores[] = h.split(":");
			horas = Integer.parseInt(valores[0]);
			minutos = Integer.parseInt(valores[1]);
		}

		TimePickerDialog hora = new TimePickerDialog(this,
				new OnTimeSetListener() {

					@Override
					public void onTimeSet(TimePicker view, int hourOfDay,
							int minute) {
						c.set(Calendar.HOUR_OF_DAY, hourOfDay);
						c.set(Calendar.MINUTE, minute);
						SimpleDateFormat sdf = new SimpleDateFormat("HH:mm",
								Locale.US);
						et.setText(sdf.format(c.getTime()));
						if (et == etHIC10 || et == etHTC10) {
							CalcularMinutos(etHIC10, etHTC10, etTC10);
						}
						if (et == etHIC10_1 || et == etHTC10_1) {
							CalcularMinutos(etHIC10_1, etHTC10_1, etTC10_1);
						}
					}
				}, horas, minutos, true);

		hora.setButton(DatePickerDialog.BUTTON_NEUTRAL, "Borrar",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						LimpiarTotales(et);
					}
				});
		hora.show();
	}

	private void CalcularMinutos(EditText eti, EditText ett, EditText tm) {
		int li = eti.getText().toString().length();
		int lt = ett.getText().toString().length();

		if (li != 0 && lt != 0) {
			String inicio = "00/00/0000 " + eti.getText().toString();
			String termino = "00/00/0000 " + ett.getText().toString();
			SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy HH:mm");
			try {
				Date ini = formato.parse(inicio);
				Date ter = formato.parse(termino);
				tm.setText((ter.getTime() - ini.getTime()) / 60000 + "");
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
	}

	private void LimpiarTotales(EditText et) {
		et.setText("");
		if (et == etHIC10 || et == etHTC10) {
			etTC10.setText("");
		}
		if (et == etHIC10_1 || et == etHTC10_1) {
			etTC10_1.setText("");
		}
	}
}
